﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Common
{
    public static class Tree
    { 

        public static void GetTree(DataTable dt, DataRow[] dataRow, ref List<VM_Tree> VM_TreeList, string Field_IdName, string Field_ParentIdName, string Field_Name)
        {
            foreach (var item in dataRow)
            {
                VM_Tree VM_Tree = new VM_Tree();
                VM_Tree.id = Convert.ToInt32(item[Field_IdName].ToString());
                VM_Tree.title = item[Field_Name].ToString();
                VM_Tree.children = new List<VM_Tree>();
                VM_TreeList.Add(VM_Tree);
                DataRow[] dataRowChild = dt.Select(string.Format(@"" + Field_ParentIdName + "={0}", VM_Tree.id));
                if (dataRowChild.Length > 0)
                {
                    GetTree(dt, dataRowChild, ref VM_Tree.children, Field_IdName, Field_ParentIdName, Field_Name);
                }
            }
        }

        /// <summary>
        /// 一级树列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<VM_Tree> GetVM_TreeList(DataTable dt)
        {
            var List = new List<VM_Tree>();
            foreach (DataRow item in dt.Rows)
            {
                VM_Tree vM_Tree = new VM_Tree();
                vM_Tree.title =item["DepName"].ToString()??"";
                vM_Tree.id = Convert.ToInt32(item["DepID"].ToString() ?? "0");
                List.Add(vM_Tree);
            }
            return List;
        }
        public static List<VM_Tree_2> GetVM_TreeList_User(DataTable dt)
        {
            var List = new List<VM_Tree_2>();
            string gName = "";
            VM_Tree_2 vM_Tree = new VM_Tree_2();
            int i = 0;
            foreach (DataRow item in dt.Rows)
            {
                if (gName!= item["Groups"].ToString())
                {
                    if (gName!="")
                    {
                        List.Add(vM_Tree);
                        vM_Tree = new VM_Tree_2();
                    }
                    gName = item["Groups"].ToString();
                    vM_Tree.title = item["Groups"].ToString() ?? "";
                    vM_Tree.id = "";
                }
                VM_Tree_2 vM_Treec = new VM_Tree_2();
                vM_Treec.title = item["UserName"].ToString() ?? "";
                vM_Treec.id = item["UserID"].ToString();
                vM_Tree.children.Add(vM_Treec);
                if (i==dt.Rows.Count-1)
                {
                    List.Add(vM_Tree);
                    vM_Tree = new VM_Tree_2();
                }
                i++;
            }
            return List;
        }
    }


    public class VM_Tree
    {
        public string title { get; set; }
        public int id { get; set; }
        public Boolean spread = true;
        public List<VM_Tree> children = new List<VM_Tree>();
    }

    public class VM_Tree_2
    {
        public string title { get; set; }
        public string id { get; set; }
        public Boolean spread = true;
        public List<VM_Tree_2> children = new List<VM_Tree_2>();
    }
}
